﻿namespace GenericListProgram
{
    using System;

    public static class GenericListExtensions
    {
        public static T Min<T>(this GenericList<T> genericList)
            where T : IComparable<T>
        {
            T min = genericList[0];
            if (genericList.Count == 0)
            {
                throw new InvalidOperationException("Sequence must not be empty.");
            }
            else if (genericList.Count != 1)
            {
                for (int index = 1; index < genericList.Count; index++)
                {
                    if (genericList[index].CompareTo(min) < 0)
                    {
                        min = genericList[index];
                    }
                }
            }

            return min;
        }

        public static T Max<T>(this GenericList<T> genericList)
            where T : IComparable<T>
        {
            T max = genericList[0];
            if (genericList.Count == 0)
            {
                throw new InvalidOperationException("Sequence must not be empty.");
            }
            else if (genericList.Count != 1)
            {
                for (int index = 0; index < genericList.Count; index++)
                {
                    if (genericList[index].CompareTo(max) > 0)
                    {
                        max = genericList[index];
                    }
                }
            }

            return max;
        }
    }
}